

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using Img = System.Drawing;
using System.IO;
using JTC.MiniCommerce.Core;
using JTC.MiniCommerce.Core.IO;
using JTC.MiniCommerce.Localization;
using SubSonic.Utilities;

namespace JTC.MiniCommerce.Web.admin {
  public partial class imageselector : System.Web.UI.Page {

    #region Constants

    private const string CATEGORY_IMAGE_PATH = @"~/repository/category/";
    private const string PRODUCT_IMAGE_PATH = @"~/repository/product/";
    private const string SITE_IMAGE_PATH = @"~/repository/site/";

    #endregion

    #region Member Variables

    private string view = string.Empty;
    private string controlId = string.Empty;
    private List<Image> imageList = new List<Image>();
    private string path = string.Empty;

    #endregion

    #region Page Events

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e) {
      try {
        controlId = Utility.GetParameter("controlId");
        view = Utility.GetParameter("view");
        SetImageSelectorProperties();
        switch(view) {
          case "c":
            pnlImages.GroupingText = LocalizationUtility.GetText("lblProductCategoryImages");
            path = CATEGORY_IMAGE_PATH;
            break;
          case "p":
            pnlImages.GroupingText = LocalizationUtility.GetText("lblProductImages");
            path = PRODUCT_IMAGE_PATH;
            break;
          case "s":
            pnlImages.GroupingText = LocalizationUtility.GetText("lblSiteImages");
            path = SITE_IMAGE_PATH;
            break;
        }
        if(!Page.IsPostBack) {
          LoadImageList();
        }
      }
      catch(Exception ex) {
        Logger.Error(typeof(imageselector).Name + ".Page_Load", ex);
        Master.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }
    }

    /// <summary>
    /// Handles the Click event of the btnUpload control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void btnUpload_Click(object sender, EventArgs e) {
      try {
        HttpPostedFile file = fuFile.PostedFile;
        if(file.ContentLength > 0) {
          FileWriter fileWriter = new FileWriter();
          string finalPath = HttpContext.Current.Server.MapPath(path) + fuFile.FileName;
          fileWriter.Write(finalPath, file.InputStream);
          LoadImageList();
          Master.MessageCenter.DisplaySuccessMessage(LocalizationUtility.GetText("lblImageSaved"));
        }
      }
      catch(Exception ex) {
        Logger.Error(typeof(imageselector).Name + ".btnUpload_Click", ex);
        Master.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }
    }

    #endregion

    #region Methods

    #region Private

    /// <summary>
    /// Loads the image list.
    /// </summary>
    private void LoadImageList() {
      FetchImageList(path);
      dlImages.DataSource = imageList;
      dlImages.DataBind();
    }

    /// <summary>
    /// Fetches the image list.
    /// </summary>
    /// <param name="path">The path.</param>
    private void FetchImageList(string path) {
      string mappedPath = HttpContext.Current.Server.MapPath(path);
      string[] images = Directory.GetFiles(mappedPath);
      Image image;
      Img.Image drawnImage;
      //bool isValid = false;
      foreach(string img in images) {
        drawnImage = Img.Image.FromFile(img);
        image = new Image();
        image.ImageUrl = path + Path.GetFileName(img);
        image.Attributes.Add("ondblclick", "javascript:window.top.document.getElementById(\'" + controlId + "\').value=\'" + image.ImageUrl + "\';window.top.hidePopWin(true);");
        //do a little simple scaling
        //landscape
        if(drawnImage.Width > drawnImage.Height) {
          if(drawnImage.Width > 90) {
            image.Width = 90;
            image.Height = drawnImage.Height * 90 / drawnImage.Width;
          }
        }
        else { //portrait
          if(drawnImage.Height > 90) {
            image.Height = 90;
            image.Width = drawnImage.Width * 90 / drawnImage.Height;
          }
        }
        imageList.Add(image);
      }
    }

    /// <summary>
    /// Sets the image selector properties.
    /// </summary>
    private void SetImageSelectorProperties() {
      this.Title = LocalizationUtility.GetText("titleImageSelector");
      LocalizationUtility.AddPanelText(pnlImageManagement);
      LocalizationUtility.AddButtonText(btnUpload);
    }

    #endregion

    #endregion

  }
}
